package view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import ai.BestChoiceType;
import model.GameConfiguration;
import model.PlayerType;

public class ConfigurationFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7729330008468211136L;
	private JPanel contentPane;
	private JButton btnAvvia;
	private JRadioButton rdbtnUmano1, rdbtnUmano2, rdbtnComputer1,
			rdbtnComputer2;
	private JComboBox<String> comboBoxAlg1, comboBoxAlg2, comboBoxProf1,
			comboBoxProf2, comboBoxRighe, comboBoxColonne, comboBoxBChoice1,
			comboBoxBChoice2;
	private JPanel computerPanel1, computerPanel2;
	private JCheckBox chckbxThreatFinder1, chckbxThreatFinder2,
			chckbxThreatBonus1, chckbxDoubleThreat1, chckbxTactics1,
			chckbxThreatBonus2, chckbxDoubleThreat2, chckbxTactics2,
			chckbxCheckDepth1, chckbxCheckDepth2, chckbxPotentialthreat1,
			chckbxPotentialthreat2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		new ConfigurationFrame();
	}

	/**
	 * Create the frame.
	 */
	public ConfigurationFrame() {
		initGUI();

		btnAvvia.addActionListener(new StartMatchActionListener());
	}

	private void initGUI() {
		setTitle("Forza 4 - Impostazioni partita");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 541);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblConfiguraUnaNuova = new JLabel(
				"Configura una nuova partita di Forza 4!");
		lblConfiguraUnaNuova.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblConfiguraUnaNuova.setBounds(139, 6, 322, 40);
		contentPane.add(lblConfiguraUnaNuova);

		btnAvvia = new JButton("Avvia");
		btnAvvia.setBounds(505, 490, 89, 23);
		contentPane.add(btnAvvia);

		ButtonGroup group = new ButtonGroup();
		ButtonGroup group2 = new ButtonGroup();

		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null,
				null));
		panel.setBounds(6, 146, 284, 340);
		contentPane.add(panel);
		panel.setLayout(null);

		JLabel lblGiocatore = new JLabel("Giocatore 1");
		lblGiocatore.setBounds(17, 16, 65, 14);
		panel.add(lblGiocatore);
		lblGiocatore.setFont(new Font("Tahoma", Font.BOLD, 11));

		rdbtnUmano1 = new JRadioButton("Umano");
		rdbtnUmano1.setBounds(27, 42, 109, 23);
		panel.add(rdbtnUmano1);
		group.add(rdbtnUmano1);

		rdbtnComputer1 = new JRadioButton("Computer");

		rdbtnComputer1.setBounds(27, 69, 109, 23);
		panel.add(rdbtnComputer1);
		rdbtnComputer1.setSelected(true);
		group.add(rdbtnComputer1);

		computerPanel1 = new JPanel();
		computerPanel1.setBorder(new TitledBorder(null, "Computer settings",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		computerPanel1.setBounds(6, 95, 272, 239);
		panel.add(computerPanel1);
		computerPanel1.setLayout(null);

		JLabel lblAlgoritmo = new JLabel("Algoritmo:");
		lblAlgoritmo.setBounds(17, 24, 73, 14);
		computerPanel1.add(lblAlgoritmo);
		lblAlgoritmo.setHorizontalAlignment(SwingConstants.RIGHT);

		comboBoxAlg1 = new JComboBox<String>();
		comboBoxAlg1.setBounds(92, 22, 171, 20);
		computerPanel1.add(comboBoxAlg1);
		comboBoxAlg1.setModel(new DefaultComboBoxModel(new String[] {"Random", "MiniMax", "ABPruning"}));
		comboBoxAlg1.setSelectedIndex(2);

		JLabel lblProfondit = new JLabel("Profondit\u00E0:");
		lblProfondit.setBounds(17, 50, 73, 14);
		computerPanel1.add(lblProfondit);
		lblProfondit.setHorizontalAlignment(SwingConstants.RIGHT);

		comboBoxProf1 = new JComboBox<String>();
		comboBoxProf1.setBounds(92, 48, 171, 20);
		computerPanel1.add(comboBoxProf1);
		comboBoxProf1.setModel(new DefaultComboBoxModel<String>(new String[] {
				"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
				"12" }));
		comboBoxProf1.setSelectedIndex(10);

		chckbxCheckDepth1 = new JCheckBox("Check depth");
		chckbxCheckDepth1.setSelected(true);
		chckbxCheckDepth1.setBounds(92, 95, 128, 23);
		computerPanel1.add(chckbxCheckDepth1);

		JLabel lblBestChoice = new JLabel("Best choice:");
		lblBestChoice.setBounds(14, 77, 76, 14);
		computerPanel1.add(lblBestChoice);
		lblBestChoice.setHorizontalAlignment(SwingConstants.RIGHT);

		comboBoxBChoice1 = new JComboBox<String>();
		comboBoxBChoice1.setBounds(92, 75, 171, 20);
		computerPanel1.add(comboBoxBChoice1);
		comboBoxBChoice1.setModel(new DefaultComboBoxModel<String>(
				new String[] { "Random", "Centered", "Gaussian" }));
		comboBoxBChoice1.setSelectedIndex(1);

		chckbxThreatFinder1 = new JCheckBox("Threat finder:");
		chckbxThreatFinder1.setSelected(true);
		chckbxThreatFinder1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (chckbxThreatFinder1.isSelected()) {
					chckbxDoubleThreat1.setEnabled(true);
					chckbxTactics1.setEnabled(true);
					chckbxThreatBonus1.setEnabled(true);
				} else {
					chckbxDoubleThreat1.setEnabled(false);
					chckbxTactics1.setEnabled(false);
					chckbxThreatBonus1.setEnabled(false);
				}
			}
		});

		chckbxThreatFinder1.setBounds(6, 127, 128, 23);
		computerPanel1.add(chckbxThreatFinder1);

		chckbxThreatBonus1 = new JCheckBox("Threat bonus");
		chckbxThreatBonus1.setSelected(true);
		chckbxThreatBonus1.setBounds(135, 127, 128, 23);
		computerPanel1.add(chckbxThreatBonus1);

		chckbxDoubleThreat1 = new JCheckBox("Double threat");
		chckbxDoubleThreat1.setSelected(true);
		chckbxDoubleThreat1.setBounds(135, 149, 128, 23);
		computerPanel1.add(chckbxDoubleThreat1);

		chckbxTactics1 = new JCheckBox("Tactics");
		chckbxTactics1.setSelected(true);
		chckbxTactics1.setBounds(135, 170, 128, 23);
		computerPanel1.add(chckbxTactics1);

		chckbxPotentialthreat1 = new JCheckBox("Potential Threat");
		chckbxPotentialthreat1.setSelected(true);
		chckbxPotentialthreat1.setBounds(6, 196, 145, 23);
		computerPanel1.add(chckbxPotentialthreat1);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
				null, null));
		panel_1.setLayout(null);
		panel_1.setBounds(310, 146, 284, 340);
		contentPane.add(panel_1);

		JLabel lblGiocatore_1 = new JLabel("Giocatore 2");
		lblGiocatore_1.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblGiocatore_1.setBounds(17, 16, 65, 14);
		panel_1.add(lblGiocatore_1);

		rdbtnUmano2 = new JRadioButton("Umano");
		rdbtnUmano2.setBounds(27, 42, 109, 23);
		panel_1.add(rdbtnUmano2);
		group2.add(rdbtnUmano2);

		rdbtnComputer2 = new JRadioButton("Computer");
		rdbtnComputer2.setSelected(true);
		rdbtnComputer2.setBounds(27, 69, 109, 23);
		panel_1.add(rdbtnComputer2);
		group2.add(rdbtnComputer2);

		computerPanel2 = new JPanel();
		computerPanel2.setBorder(new TitledBorder(null, "Computer settings",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		computerPanel2.setBounds(6, 94, 272, 240);
		panel_1.add(computerPanel2);
		computerPanel2.setLayout(null);

		JLabel label_1 = new JLabel("Algoritmo:");
		label_1.setBounds(17, 25, 73, 14);
		computerPanel2.add(label_1);
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);

		comboBoxAlg2 = new JComboBox<String>();
		comboBoxAlg2.setBounds(92, 23, 171, 20);
		computerPanel2.add(comboBoxAlg2);
		comboBoxAlg2.setModel(new DefaultComboBoxModel(new String[] {"Random", "MiniMax", "ABPruning"}));
		comboBoxAlg2.setSelectedIndex(2);

		JLabel label_2 = new JLabel("Profondit\u00E0:");
		label_2.setBounds(17, 51, 73, 14);
		computerPanel2.add(label_2);
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);

		comboBoxProf2 = new JComboBox<String>();
		comboBoxProf2.setBounds(92, 49, 171, 20);
		computerPanel2.add(comboBoxProf2);
		comboBoxProf2.setModel(new DefaultComboBoxModel<String>(new String[] {
				"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
				"12" }));
		comboBoxProf2.setSelectedIndex(10);

		chckbxCheckDepth2 = new JCheckBox("Check depth");
		chckbxCheckDepth2.setSelected(true);
		chckbxCheckDepth2.setBounds(92, 96, 128, 23);
		computerPanel2.add(chckbxCheckDepth2);

		JLabel label = new JLabel("Best choice:");
		label.setBounds(14, 78, 76, 14);
		computerPanel2.add(label);
		label.setHorizontalAlignment(SwingConstants.RIGHT);

		comboBoxBChoice2 = new JComboBox<String>();
		comboBoxBChoice2.setBounds(92, 76, 171, 20);
		computerPanel2.add(comboBoxBChoice2);
		comboBoxBChoice2.setModel(new DefaultComboBoxModel<String>(
				new String[] { "Random", "Centered", "Gaussian" }));
		comboBoxBChoice2.setSelectedIndex(1);

		chckbxThreatFinder2 = new JCheckBox("Threat finder:");
		chckbxThreatFinder2.setSelected(true);
		chckbxThreatFinder2.setBounds(6, 128, 128, 23);
		computerPanel2.add(chckbxThreatFinder2);
		chckbxThreatFinder2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (chckbxThreatFinder2.isSelected()) {
					chckbxDoubleThreat2.setEnabled(true);
					chckbxTactics2.setEnabled(true);
					chckbxThreatBonus2.setEnabled(true);
				} else {
					chckbxDoubleThreat2.setEnabled(false);
					chckbxTactics2.setEnabled(false);
					chckbxThreatBonus2.setEnabled(false);
				}
			}
		});

		chckbxThreatBonus2 = new JCheckBox("Threat bonus");
		chckbxThreatBonus2.setSelected(true);
		chckbxThreatBonus2.setBounds(135, 128, 128, 23);
		computerPanel2.add(chckbxThreatBonus2);

		chckbxDoubleThreat2 = new JCheckBox("Double threat");
		chckbxDoubleThreat2.setSelected(true);
		chckbxDoubleThreat2.setBounds(135, 150, 128, 23);
		computerPanel2.add(chckbxDoubleThreat2);

		chckbxTactics2 = new JCheckBox("Tactics");
		chckbxTactics2.setSelected(true);
		chckbxTactics2.setBounds(135, 171, 128, 23);
		computerPanel2.add(chckbxTactics2);

		chckbxPotentialthreat2 = new JCheckBox("Potential Threat");
		chckbxPotentialthreat2.setSelected(true);
		chckbxPotentialthreat2.setBounds(6, 195, 145, 23);
		computerPanel2.add(chckbxPotentialthreat2);

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
				null, null));
		panel_2.setBounds(6, 53, 588, 81);
		contentPane.add(panel_2);
		panel_2.setLayout(null);

		JLabel lblCampoDaGioco = new JLabel("Campo da gioco");
		lblCampoDaGioco.setBounds(17, 6, 88, 14);
		panel_2.add(lblCampoDaGioco);
		lblCampoDaGioco.setFont(new Font("Tahoma", Font.BOLD, 11));

		JLabel lblNewLabel = new JLabel("Dimensione:");
		lblNewLabel.setBounds(40, 34, 109, 14);
		panel_2.add(lblNewLabel);

		JLabel lblRighedefault = new JLabel("Righe (default 6):");
		lblRighedefault.setBounds(131, 34, 109, 14);
		panel_2.add(lblRighedefault);

		comboBoxRighe = new JComboBox<String>();
		comboBoxRighe.setBounds(245, 32, 72, 20);
		panel_2.add(comboBoxRighe);
		comboBoxRighe.setModel(new DefaultComboBoxModel<String>(new String[] {
				"4", "6", "7", "8", "9", "10" }));
		comboBoxRighe.setSelectedIndex(1);

		JLabel lblColonnedefault = new JLabel("Colonne (default 7):");
		lblColonnedefault.setBounds(329, 31, 135, 20);
		panel_2.add(lblColonnedefault);

		comboBoxColonne = new JComboBox<String>();
		comboBoxColonne.setBounds(467, 32, 72, 20);
		panel_2.add(comboBoxColonne);
		comboBoxColonne.setModel(new DefaultComboBoxModel<String>(new String[] {
				"4", "7", "8", "9", "10", "11", "12", "13" }));
		comboBoxColonne.setSelectedIndex(1);
		rdbtnComputer1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				computerPanel1.setVisible(true);

			}
		});
		rdbtnUmano1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				computerPanel1.setVisible(false);

			}
		});
		rdbtnComputer2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				computerPanel2.setVisible(true);

			}
		});
		rdbtnUmano2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				computerPanel2.setVisible(false);

			}
		});

		setLocationRelativeTo(null);
		setVisible(true);

	}

	private class StartMatchActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			int rows = Integer.parseInt(comboBoxRighe.getSelectedItem()
					.toString());
			int cols = Integer.parseInt(comboBoxColonne.getSelectedItem()
					.toString());
			PlayerType p1, p2;
			int depth1, depth2;
			boolean enDepthCheck1, enDepthCheck2;
			boolean enThreatFinder1, enThreatFinder2;
			boolean enTB1, enTB2;
			boolean enDT1, enDT2;
			boolean enTactics1, enTactics2;
			boolean enPotential1, enPotential2;
			BestChoiceType choiceType1, choiceType2;
			choiceType1 = BestChoiceType.CENTERED;
			choiceType2 = BestChoiceType.CENTERED;
			if (rdbtnComputer1.isSelected()) {
				p1 = getPlayerTypeFormText(comboBoxAlg1.getSelectedItem()
						.toString());
				depth1 = Integer.parseInt(comboBoxProf1.getSelectedItem()
						.toString());
				enDepthCheck1 = chckbxCheckDepth1.isSelected();
				enThreatFinder1 = chckbxThreatFinder1.isSelected();
				enTB1 = chckbxThreatBonus1.isSelected();
				enDT1 = chckbxDoubleThreat1.isSelected();
				enTactics1 = chckbxTactics1.isSelected();
				enPotential1  =chckbxPotentialthreat1.isSelected();
				
				switch (comboBoxBChoice1.getSelectedItem().toString()) {
				case "Random":
					choiceType1 = BestChoiceType.RANDOM;
					break;
				case "Centered":
					choiceType1 = BestChoiceType.CENTERED;
					break;
				case "Gaussian":
					choiceType1 = BestChoiceType.GAUSSIAN;
					break;
				}
			} else {
				p1 = PlayerType.HUMAN;
				depth1 = 0;
				enDepthCheck1 = false;
				enThreatFinder1 = false;
				enTB1 = false;
				enDT1 = false;
				enTactics1 = false;
				enPotential1 = false;
			}

			if (rdbtnComputer2.isSelected()) {
				p2 = getPlayerTypeFormText(comboBoxAlg2.getSelectedItem()
						.toString());
				depth2 = Integer.parseInt(comboBoxProf2.getSelectedItem()
						.toString());
				enDepthCheck2 = chckbxCheckDepth2.isSelected();
				enThreatFinder2 = chckbxThreatFinder2.isSelected();
				enTB2 = chckbxThreatBonus2.isSelected();
				enDT2 = chckbxDoubleThreat2.isSelected();
				enTactics2 = chckbxTactics2.isSelected();
				enPotential2  =chckbxPotentialthreat2.isSelected();
				
				switch (comboBoxBChoice2.getSelectedItem().toString()) {
				case "Random":
					choiceType2 = BestChoiceType.RANDOM;
					break;
				case "Centered":
					choiceType2 = BestChoiceType.CENTERED;
					break;
				case "Gaussian":
					choiceType2 = BestChoiceType.GAUSSIAN;
					break;
				}
			} else {
				p2 = PlayerType.HUMAN;
				depth2 = 0;
				enDepthCheck2 = false;
				enThreatFinder2 = false;
				enTB2 = false;
				enDT2 = false;
				enTactics2 = false;
				enPotential2 = false;
			}

			GameConfiguration configuration = new GameConfiguration(rows, cols,
					p1, p2, depth1, depth2, enDepthCheck1, enDepthCheck2,
					enThreatFinder1, enThreatFinder2, enPotential1, enPotential2, enTB1, enTB2, enDT1,
					enDT2, enTactics1, enTactics2, choiceType1, choiceType2, true, true);
			new GameFrame(configuration);
			dispose();

		}

		private PlayerType getPlayerTypeFormText(String playerType) {
			PlayerType p;
			switch (playerType) {
			case "Random":
				p = PlayerType.RANDOM;
				break;
			case "MiniMax":
				p = PlayerType.MINIMAX;
				break;
			case "ABPruning":
				p = PlayerType.ABPRUNING;
				break;
			default:
				p = PlayerType.RANDOM;
			}
			return p;
		}
	}
}
